package com.wlt.service;

import com.wlt.pojo.CartGoods;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
@DubboService
public class CartServiceImpl implements CartService {
    @Autowired
    private RedisTemplate redisTemplate;
    
    @Override
    public void addCart (Long userId, CartGoods cartGoods) {
        // 根据id获取用户购物车列表
        List<CartGoods> cartList = findCartList(userId);
        
        // 查询购物车是否有该商品，如果有则添加商品数量
        for (CartGoods cartGood : cartList) {
            if (cartGoods.getGoodId().equals(cartGood.getGoodId())) {
                int newNem = cartGood.getNum() + cartGoods.getNum();
                
                cartGood.setNum(newNem);
                
                redisTemplate.boundHashOps("cartList").put(userId, cartGood);
                
                return;
            }
        }
        
        // 如果没有则添加到购物车列表
        cartList.add(cartGoods);
        
        redisTemplate.boundHashOps("cartList").put(userId, cartList);
    }
    
    @Override
    public void handleCart (Long userId, Long goodId, Integer num) {
        // 获取购物车列表
        List<CartGoods> cartList = findCartList(userId);
        
        // 遍历列表找到对应商品
        for (CartGoods cartGood : cartList) {
            if (goodId.equals(cartGood.getGoodId())) {
                // 改变数量
                cartGood.setNum(num);
                
                break;
            }
        }
        
        // 将新的购物车列表保存到Redis
        redisTemplate.boundHashOps("cartList").put(userId, cartList);
    }
    
    @Override
    public void deleteCart (Long userId, Long goodId) {
        // 获取购物车列表
        List<CartGoods> cartList = findCartList(userId);
        
        // 移除商品
        for (CartGoods cartGood : cartList) {
            if (goodId.equals(cartGood.getGoodId())) {
                cartList.remove(cartGood);
                
                break;
            }
        }
        
        // 将新的购物车列表保存到Redis
        redisTemplate.boundHashOps("cartList").delete(userId, cartList);
    }
    
    @Override
    public List<CartGoods> findCartList (Long userId) {
        // 从Redis中查询用户购物车列表
        Object cartList = redisTemplate.boundHashOps("cartList").get(userId);
        
        // 购物车添加过数据（能查到直接返回）
        if (cartList == null) {
            return new ArrayList<CartGoods>();
        } else {
            // 没添加过数据（直接返回）
            return (List<CartGoods>) cartList;
        }
    }
    
    @Override
    public void refreshCartGoods (CartGoods cartGoods) {
        // 获取所有用户的购物车
        BoundHashOperations cartList = redisTemplate.boundHashOps("cartList");
        Map<Long, List<CartGoods>> allCartGoods = cartList.entries();
        Collection<List<CartGoods>> values = allCartGoods.values();
        
        // 遍历所有用户的购物车
        for (List<CartGoods> goodsList : values) {
            // 遍历一个用户的所有商品
            for (CartGoods cartGood : goodsList) {
                // 如果该商品是更新商品则对其进行更新
                if (cartGood.getGoodId().equals(cartGoods.getGoodId())) {
                    cartGood.setGoodsName(cartGoods.getGoodsName());
                    cartGood.setHeaderPic(cartGoods.getHeaderPic());
                    cartGood.setPrice(cartGoods.getPrice());
                }
            }
        }
        
        // 重新同步到Redis
        redisTemplate.delete("cartList");
        redisTemplate.boundHashOps("cartList").putAll(allCartGoods);
    }
    
    @Override
    public void deleteCartGoods (Long goodId) {
        // 获取所有用户的购物车
        BoundHashOperations cartList = redisTemplate.boundHashOps("cartList");
        Map<Long, List<CartGoods>> allCartGoods = cartList.entries();
        Collection<List<CartGoods>> values = allCartGoods.values();
        
        // 遍历所有
        for (List<CartGoods> goodsList : values) {
            // 遍历每个用户的购物车
            // 如果有则删除
            goodsList.removeIf(cartGood -> goodId.equals(cartGood.getGoodId()));
        }
        
        // 同步到Redis
        redisTemplate.delete("cartList");
        redisTemplate.boundHashOps("cartList").putAll(allCartGoods);
    }
}
